{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import polars as pl\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from buckaroo.polars_buckaroo import PolarsBuckarooWidget\n",
    "PBW = PolarsBuckarooWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "str_df = pl.DataFrame({\n",
    "    'a': [\"foo\", \"foobar\", \"baz\",  None,  None], \n",
    "    'b': [    1,        2,     3,  None,  None], \n",
    "    'c': [ None,     None,  None, \"foo\", \"baz\"],\n",
    "    'd': np.arange(5),\n",
    "    'e': [    2,       -5,    30,     1,    40]\n",
    "})\n",
    "bw = PBW(str_df)\n",
    "bw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "bw.ac_obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "def or_join(a,b):\n",
    "    return a|b\n",
    "def str_filter_df(df, needle):\n",
    "    str_cols = df.select(pl.col(pl.String)).columns\n",
    "    clauses = [pl.col(c).str.contains(needle) for c in str_cols]\n",
    "    orred_clause = reduce(or_join, clauses)\n",
    "    return df.filter(orred_clause)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "str_filter_df(str_df, \"baz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "str_filter_df(str_df, \"ba\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "str_df.filter(pl.col('e').gt(1), pl.col('e').lt(35))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def range_filter_df(df, col, lower, upper):\n",
    "    clauses = []\n",
    "    if lower is not None:\n",
    "        clauses.append(pl.col(col).gt(lower))\n",
    "    if upper is not None:\n",
    "        clauses.append(pl.col(col).lt(upper))\n",
    "    return df.filter(*clauses)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "range_filter_df(str_df, 'e', 0, None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from functools import reduce"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def or_join(a,b):\n",
    "    return a|b\n",
    "reduce(or_join, [pl.col(c).str.contains('foo') for c in str_df.select(pl.col(pl.String)).columns])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "def or_join(a,b):\n",
    "    return a|b\n",
    "def str_filter_df(df, needle):\n",
    "    str_cols = df.select(pl.col(pl.String)).columns\n",
    "    clauses = [pl.col(c).str.contains(needle) for c in str_cols]\n",
    "    orred_clause = reduce(or_join, clauses)\n",
    "    return df.filter(orred_clause)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "str_df.filter(reduce(or_join, [pl.col(c).str.contains('foo') for c in str_df.select(pl.col(pl.String)).columns]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "r[pl.col(c).str.contains('foo') for c in str_df.select(pl.col(pl.String)).columns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "str_df.filter(*[pl.col(c).str.contains('foo') for c in str_df.select(pl.col(pl.String)).columns])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#str_df.filter(F.col(pl.String))\n",
    "str_df.filter(pl.col('a').str.contains('foo')|pl.col('c').str.contains('foo'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from buckaroo.polars_buckaroo import PolarsBuckarooWidget\n",
    "PBW = PolarsBuckarooWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
